package com.fang.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.fang.entity.Cart;
import com.fang.entity.User;
import com.fang.service.CartService;
import com.google.gson.Gson;

public class CartServlet extends BaseServlet {
	
	CartService service = new CartService();
	
	
//	改变购物车数量
	public void changnum(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		User loginuser = (User) req.getSession().getAttribute("loginuser");
		if (loginuser == null) {
//			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			resp.getWriter().write("tologin");
		}else{
			Cart cart = new Cart();
			try {
				BeanUtils.populate(cart, req.getParameterMap());
				cart.setUid(loginuser.getId());
				service.changnum(cart);
				resp.getWriter().write("ok");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
//	删除购物车货物
	public void delcart(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 判断用户是否登录
				User loginuser = (User) req.getSession().getAttribute("loginuser");
				if (loginuser == null) {
//					resp.sendRedirect(req.getContextPath() + "/login.jsp");
					resp.getWriter().write("tologin");
				}else{
					Cart cart = new Cart();
					String gid = req.getParameter("gid");
					cart.setGid(Integer.parseInt(gid));
					cart.setUid(loginuser.getId());
					service.delete(cart);
//					resp.sendRedirect(req.getContextPath()+"/cart?method=query");
					resp.getWriter().write("ok");
				}
	}
	
	
//添加购物车
	public void addcart(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 判断用户是否登录
		User loginuser = (User) req.getSession().getAttribute("loginuser");
		if (loginuser == null) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		} else {
			// 接收数据封装对象
			Cart cart = new Cart();
			try {
				BeanUtils.populate(cart, req.getParameterMap());
				cart.setUid(loginuser.getId());

				// 调用service，后台查询
				service.addcart(cart);
				resp.sendRedirect(req.getContextPath() + "/buyorcart.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**购物车当前用户商品展示
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void query(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			// 判断用户是否登录
			User loginuser = (User) req.getSession().getAttribute("loginuser");
			if (loginuser == null) {
				resp.sendRedirect(req.getContextPath() + "/login.jsp");
			} else {
				
			int uid = loginuser.getId();
				List<Cart> clist = service.querybyuid(uid);
				req.setAttribute("clist", clist);
				String to = req.getParameter("to");
				if("orders".equals(to)){
					req.getRequestDispatcher("orders_submit.jsp").forward(req, resp);
				}else{
					
					req.getRequestDispatcher("cart.jsp").forward(req, resp);
				}
			}
	}
	
	public void querynum(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			// 判断用户是否登录
			User loginuser = (User) req.getSession().getAttribute("loginuser");
			if (loginuser == null) {
				resp.sendRedirect(req.getContextPath() + "/login.jsp");
			} else {
				int uid = loginuser.getId();
				List<Cart> clist = service.querybyuid(uid);
				Gson gson = new Gson();
				String json = gson.toJson(clist);
				resp.getWriter().write(json);
				
			}
	}
}